package com.hushao.lims.weblims.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author 930588706
 * @date 18/3/1
 */
public class DateUtil {
    /**
     * 根据日期判断出星期几
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getWeek(int year, int month, int day){
        if(month == 1 || month == 2)
        {
            month += 12;
            year--;
        }
        int c = year / 100;
        int y = year % 100;
        int m = month;
        int d = day;
        int W = c / 4 - 2 * c + y + y / 4 + 26 * (m + 1) / 10 + d - 1;

        int ans;

        if(W < 0){
            ans = (W + (-W / 7 + 1) * 7) % 7;}
        else{ ans = W % 7;}
        if(ans == 0) { return ans + 7;}
        return ans;
    }

    /**
     * 根据当前时间判断学期
     * @return
     */
    public static String getTermByThisDate(){
        //java8 获取当前时间并格式化
        LocalDate localDate=LocalDate.now();
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyyMMdd");
        String currentTime=localDate.format(dateTimeFormatter);
        //根据当前时间判断在哪一学期
        String term;
        if(Integer.parseInt(currentTime.substring(4,6))>9){
            term=currentTime.substring(0,4)+(Integer.parseInt(currentTime.substring(0,4))+1)+"01";
        }else { term=currentTime.substring(0,4)+currentTime.substring(0,4)+"07";}
        return term;
    }
}
